var myApp =
    angular.module('myApp', ['ui.router','ui.validate','ui.bootstrap','ngRoute']).config(function($stateProvider, $urlRouterProvider) {

	  $urlRouterProvider.otherwise("/login");
	  
	  $stateProvider
        .state('hoteles', {
          url: "/hoteles",
          views: {
              "headerView": { templateUrl: "views/menuBar.html",controller: "BarCtrl" },
              "indexView": { templateUrl: "views/hoteles.html",controller: "HomeCtrl" }
          },
          access: {isFree: false}
        })
          .state('hotel', {
              url: "/hotel",
              views: {
                  "headerView": { templateUrl: "views/menuBar.html",controller: "BarCtrl" },
                  "indexView": { templateUrl: "views/crearHotel.html",controller: "HotelCtrl" }
              },
              access: {isFree: false}
          })
          .state('hotelesId', {
              url: "/hotel/:id",
              views: {
                  "headerView": { templateUrl: "views/menuBar.html",controller: "BarCtrl" },
                  "indexView": { templateUrl: "views/hotel.html",controller: "HotelProfileCtrl" }
              },
              access: {isFree: false}
          })
	    .state('login', {
	      url: "/login",
	      views: {
	            "indexView": { templateUrl: "views/login.html",controller: "LoginCtrl" }
	      },
	      access: {isFree: true}
	    })
        .state('/', {
             url: "/",
             access: {isFree: false}
        })
	  });

myApp.factory('UserFactory', function() {
    var User = {
            username: '',
            password: ''
    };

    User.setUsername = function(username) {
            User.username = username;
    }

    User.getUsername = function() {
        return User.username;
    }

    User.getPassword = function() {
        return User.password;
    }

    User.setPassword = function(password) {
            User.password = password;
    }

    User.getUser = function(){
        return User;
    }

    User.remove = function(){
        User.setUsername('');
        User.setPassword('');
    }

    return User;
});
myApp.directive('ngCheckurl', ['$rootScope', '$http', '$location','UserFactory', function ($root, $http, $location, UserFactory) {
    return {
        link: function (scope, elem, attrs, ctrl) {
            $root.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {

                if(toState.access.isFree){
                        console.log("Aqui estoy")
                        if ($.cookie('coti')) {
                            console.log("ahora aqui estoy")
                            $location.path("/hoteles");
                        }
                }else {
                    console.log("entre aqui")
                    if (!$.cookie('coti')) {
                        console.log("y ahora por aqui ")
                        $location.path("/login");
                    }
                }
            })
        }
    }
}]);



